﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.DynamicData;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;

namespace DD_Extensions
{
    public partial class ListViewPager : System.Web.UI.UserControl
    {
        private DropDownList _dropDownListPageSize;
        private MetaTable _table;
        private TableOptionsAttribute _tableOptions;
        private ListView _listView;

        protected void Page_Init(object sender, EventArgs e)
        {
            // get the list view
            _listView = this.GetParent<ListView>();
            //var table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            _table = _listView.Page.FindControlRecursive<EntityDataSource>(_listView.DataSourceID).GetMetaTable();
            _tableOptions = _table.GetAttributeOrDefault<TableOptionsAttribute>();
            DataPager1.PageSize = _tableOptions.PageSize;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //protected void DropDownListPageSize_Loaded(object sender, EventArgs e)
        //{
        //}

        //protected override void OnPreRender(EventArgs e)
        //{
        //    base.OnPreRender(e);

        //    _dropDownListPageSize = DataPager1.FindControlRecursive<DropDownList>("DropDownListPageSize");
        //    if (_tableOptions.PagerValues.Length != _dropDownListPageSize.Items.Count)
        //    {
        //        _dropDownListPageSize.Items.Clear();
        //        foreach (var item in _table.GetAttributeOrDefault<TableOptionsAttribute>().PagerValues)
        //        {
        //            _dropDownListPageSize.Items.Add(new ListItem(item));
        //        }
        //    }

        //    if (!Page.IsPostBack && _tableOptions.PageSize > 0)
        //    {
        //        var item = _dropDownListPageSize.Items.FindByValue(_tableOptions.PageSize.ToString());
        //        if (item != null)
        //            _dropDownListPageSize.SelectedValue = item.Value;
        //    }
        //}

        protected void TemplatePagerField_OnPagerCommand(object sender, DataPagerCommandEventArgs e)
        {
            int newIndex;
            int lastPageNumber;
            int pageSize = Convert.ToInt32(((DropDownList)e.Item.FindControl("DropDownListPageSize")).SelectedValue);
            int rowCount = e.TotalRowCount;

            switch (e.CommandName)
            {
                case "Next":
                    newIndex = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize;
                    if (newIndex <= e.TotalRowCount)
                        e.NewStartRowIndex = newIndex;
                    break;
                case "Previous":
                    if (e.Item.Pager.StartRowIndex != 0)
                        e.NewStartRowIndex = e.Item.Pager.StartRowIndex - pageSize;
                    break;
                case "First":
                    e.NewStartRowIndex = 0;
                    break;
                case "Last":
                    // Integer division
                    lastPageNumber = e.TotalRowCount / pageSize;
                    if ((rowCount % pageSize) == 0)
                        lastPageNumber -= 1;
                    e.NewStartRowIndex = lastPageNumber * pageSize;
                    break;
                case "Jump":
                    TextBox txtNewPageNumber = (TextBox)e.Item.FindControl("textNewPageNumber");
                    int newPageNumber = Int32.Parse(txtNewPageNumber.Text);

                    e.NewStartRowIndex = (newPageNumber - 1) * pageSize;
                    if (e.NewStartRowIndex > rowCount)
                    {
                        // Integer division
                        lastPageNumber = rowCount / pageSize;
                        if ((rowCount % pageSize) == 0)
                        {
                            lastPageNumber -= 1;
                        }
                        e.NewStartRowIndex = lastPageNumber * pageSize;
                    }
                    break;
            }
            e.NewMaximumRows = e.Item.Pager.MaximumRows;
        }

        protected void NewPageNumber_Changed(object sender, EventArgs e)
        {
            LinkButton btnJump = (LinkButton)((TextBox)sender).Parent.FindControl("buttonJump");
            ((IPostBackEventHandler)btnJump).RaisePostBackEvent(null);
        }

        protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropdownlistpagersize = (DropDownList)sender;
            DataPager1.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue);
        }
    }
}